package com.iss.ssm0531.dao.provider;

import com.iss.ssm0531.model.User;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.jdbc.SQL;

public class UserProvider {

    public String updateSQL(User user) {
        return new SQL(
        ) {
            {
                UPDATE("user");
                if (user.getUsername()!=null){
                    SET("username=#{username}");
                }
                if (user.getPassword()!=null){
                    SET("password=#{password}");
                }
                WHERE("user_id=#{userId}");
            }

        }.toString();
    }


    public String updateSQL2(User user){
        /* update user
         * set
         * <if test="username!=null">username=#{username}</if>
        * <if test="password!=null">password=#{password}</if>
        * where user_id =#{userId}
        * */
        StringBuilder stringBuilder = new StringBuilder("<script> update user ");
        stringBuilder.append(" set  ");
        stringBuilder.append(" <if test=\"username!=null\">" +
                "username=#{username}</if>  ");
        stringBuilder.append(" <if test=\"password!=null\"> , password=#{password} </if>  ");
        stringBuilder.append(" where user_id =#{userId} </script>");

        return stringBuilder.toString();

    }
}
